/*
 *  Copyright 2012 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/examples/consoleclient/client/main_wnd.h"

//#include <gdk/gdkkeysyms.h>
//#include <gtk/gtk.h>
#include <stddef.h>

#include "libyuv/convert_from.h"
#include "webrtc/examples/consoleclient/client/defaults.h"
#include "webrtc/base/common.h"
#include "webrtc/base/logging.h"
#include "webrtc/base/stringutils.h"
#include "webrtc/media/engine/webrtcvideoframe.h"
#include "webrtc/examples/consoleclient/client/zLog.h"


using rtc::sprintfn;

#if 0
namespace {

//
// Simple static functions that simply forward the callback to the
//
struct UIThreadCallbackData {
  explicit UIThreadCallbackData(MainWndCallback* cb, int id, void* d)
      : callback(cb), msg_id(id), data(d) {}
  MainWndCallback* callback;
  int msg_id;
  void* data;
};

gboolean HandleUIThreadCallback(gpointer data) {
  UIThreadCallbackData* cb_data = reinterpret_cast<UIThreadCallbackData*>(data);
  cb_data->callback->UIThreadCallback(cb_data->msg_id, cb_data->data);
  delete cb_data;
  return false;
}

}  // namespace
#endif

//
// GtkMainWnd implementation.
//

GtkMainWnd::GtkMainWnd(const char* server, int port, bool autoconnect,
                       bool autocall)
    : callback_(NULL),
      server_(server), autoconnect_(autoconnect), autocall_(autocall),
      current_ui_state(STREAMING)
{
  char buffer[10];
  sprintfn(buffer, sizeof(buffer), "%i", port);
  port_ = buffer;
  port_num_ = port;
}

GtkMainWnd::~GtkMainWnd() {
}

void GtkMainWnd::RegisterObserver(MainWndCallback* callback) {
  callback_ = callback;
}

bool GtkMainWnd::IsWindow() {
  return true;
}

void GtkMainWnd::QueueUIThreadCallback(int msg_id, void* data)
{
    RLOGD << "smile-_- !!!";
    return;
}

MainWindow::UI GtkMainWnd::current_ui() {
  return current_ui_state;
}

void GtkMainWnd::MessageBox(const char* caption, const char* text,
                            bool is_error) {
    if (is_error)
    {
        RLOGD << "error: " << text;
    }
    else
    {
        RLOGD << "warning: " << text;
    }
}

void GtkMainWnd::StartLocalRenderer(webrtc::VideoTrackInterface* local_video) {
  //local_renderer_.reset(new VideoRenderer(this, local_video));
  return;
}

void GtkMainWnd::StopLocalRenderer() {
  //local_renderer_.reset();
  return;
}

void GtkMainWnd::StartRemoteRenderer(
    webrtc::VideoTrackInterface* remote_video) {
  //remote_renderer_.reset(new VideoRenderer(this, remote_video));
  return;
}

void GtkMainWnd::StopRemoteRenderer() {
  //remote_renderer_.reset();
  return;
}

bool GtkMainWnd::Create() {
  //ASSERT(window_ == NULL);
  //SwitchToConnectUI();
  return true;
}

bool GtkMainWnd::Destroy() {
  return true;
}

void GtkMainWnd::SwitchToConnectUI() {
  current_ui_state = CONNECT_TO_SERVER;
  callback_->StartLogin(server_, port_num_);
}

void GtkMainWnd::SwitchToPeerList(const Peers& peers) {
  current_ui_state = LIST_PEERS;
  callback_->ConnectToPeer(peers.begin()->first);
}

void GtkMainWnd::SwitchToStreamingUI() {
  current_ui_state = STREAMING;
}

